﻿using STMS.STMSapp.product;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.STMSapp
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

        }
        System.Timers.Timer timer;

        // Code：1代表仓库管理、2代表产管理、3代表温度管理
        int selModuleCode = 0;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            SetTime();
            naviBtnStorage_Click(sender,e);
        }
        private void SetTime()
        {
            if (this.Tag == null)
            {
                // labelLoginUserValue.Text = this.Tag.ToString(); // 设定登录者的值
                this.labelLoginTimeValue.Text = DateTime.Now.ToString("f");
                timer = new System.Timers.Timer();
                timer.Interval = 1000; // 间隔设定为1秒
                timer.AutoReset = true;
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            }

        }
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                this.labelLoginTimeValue.Text = DateTime.Now.ToString("f");

            }));
        }

        private void ctlBtnMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void ctlBtnMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
                ctlBtnMax.Text = "1";

            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                ctlBtnMax.Text = "2";
            }
        }

        private void ctlBtnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.ExitThread();

                this.timer.Stop();
            }
        }



        private void naviBtnStorage_Click(object sender, EventArgs e)
        {
            selModuleCode = 1;
            store.FrmStoreList frmStoreList = new store.FrmStoreList();
            panelPage.AddPanelForm(frmStoreList);
            SetModuleBtnBackColor(selModuleCode);
        }

        private void naviBtnProduct_Click(object sender, EventArgs e)
        {
            selModuleCode = 2;
            product.FrmProductList frmProductList = new product.FrmProductList();
            panelPage.AddPanelForm(frmProductList);
            SetModuleBtnBackColor(selModuleCode);
        }

        private void naviBtnTemp_Click(object sender, EventArgs e)
        {
            selModuleCode = 3;
            storeTemper.FrmStoreRegionTemp frmStoreRegionTemp = new storeTemper.FrmStoreRegionTemp();
            panelPage.AddPanelForm(frmStoreRegionTemp);
            SetModuleBtnBackColor(selModuleCode);

        }

        private void SetModuleBtnBackColor(int code)
        {
            switch (code)
            {
                // 设置仓库管理背景色，其他两个颜色为默认
                case 1:
                    SetButtonColor(leftBtnStorage);
                    break;
                // 设置产品管理背景色，其他两个颜色为默认
                case 2:
                    SetButtonColor(leftBtnProduct);
                    break;
                // 设置温度管理背景色，其他两个颜色为默认
                case 3:
                    SetButtonColor(leftBtnTemp);
                    break;
            }
        }

        private void SetButtonColor(Button btn)
        {
            Color defaultColor = Color.FromArgb(0, 170, 200);
            Color selColor = Color.FromArgb(20, 210, 255);
            Button[] btnList = { leftBtnStorage, leftBtnProduct, leftBtnTemp };

            foreach (Button b in btnList)
            {
                if (b.Name != btn.Name)
                {
                    b.BackColor = defaultColor;
                }
                else
                {
                    b.BackColor = selColor;
                }
            }
        }

        private void naviBtnClose_Click(object sender, EventArgs e)
        {
            panelPage.Controls.Clear();
        }

        private void topBtnStorage_Click(object sender, EventArgs e)
        {

            store.FrmStoreList frmStoreList = new store.FrmStoreList();
            panelPage.AddPanelForm(frmStoreList);
            SetModuleBtnBackColor(selModuleCode);
        }

        private void topBtnRegion_Click(object sender, EventArgs e)
        {
            store.FrmStoreRegionList frmStoreRegionList = new store.FrmStoreRegionList();
            panelPage.AddPanelForm(frmStoreRegionList);
        }

        private void topBtnProduct_Click(object sender, EventArgs e)
        {
            product.FrmProductList frmProductList = new product.FrmProductList();
            panelPage.AddPanelForm(frmProductList);
            Button btn = sender as Button;
            //btn.ForeColor  

        }

        private void topBtnProductInRegion_Click(object sender, EventArgs e)
        {
            product.FrmProductInStore frmProductList = new product.FrmProductInStore();
            panelPage.AddPanelForm(frmProductList);

        }

        private void topBtnTemp_Click(object sender, EventArgs e)
        {
            storeTemper.FrmStoreRegionTemp frmStoreTemp = new storeTemper.FrmStoreRegionTemp();
            panelPage.AddPanelForm(frmStoreTemp);
        }
        Point point;
        bool isMove = false;
        private void panelTop_MouseDown(object sender, MouseEventArgs e)
        {
            isMove = true;
            point = e.Location;
        }

        private void panelTop_MouseUp(object sender, MouseEventArgs e)
        {
            isMove = false;
        }

        private void panelTop_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && isMove)
            {
                Point pointNew = e.Location;
                Point fPointNew = new Point(pointNew.X -point.X, pointNew.Y - point.Y);
                this.Location += new Size(fPointNew);
            }
        }
    }
}
